package com.koron.css2.serviceManage.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.AdminAreaBean;
import com.koron.css2.serviceManage.bean.ConcentratorListBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.PubAdminAreaMapper;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;
/**
 * 查询结构地址树包含集中器-水司
 * @author 甄玉晴
 *
 */
public class AdminAreaConcentratorListGroup implements ServerInterface {
	private static Logger logger = LogManager.getLogger(AdminAreaConcentratorListGroup.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

		@SuppressWarnings("rawtypes")
		MessageBean<List> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", List.class);
		 try {
			 	AdminAreaBean bean = JsonUtils.objectToPojo(req.getData(), AdminAreaBean.class);
	            PubAdminAreaMapper mapper = factory.getMapper(PubAdminAreaMapper.class);
	            List<AdminAreaBean> list = mapper.listGroup(bean);
	            //查询集中器
	            List<ConcentratorListBean> concens=mapper.listConcentratorTree(bean);
	            if(concens!=null&&concens.size()!=0) {
		            for(AdminAreaBean area:list) {
		            	List<ConcentratorListBean> concentree=new ArrayList<ConcentratorListBean>();
		            	 for(ConcentratorListBean concen:concens) {
		            		 if(concen.getAreaId().equals(area.getId())) {
		            			 concentree.add(concen);
		            		 }
		            	 }
		            	 area.setConcentrator(concentree); 
		            }
	            }
	            info.setData(list);
	        } catch (Exception e) {
	            info.setCode(Constant.MESSAGE_INT_FAIL);
	            info.setDescription(e.getMessage());
	            logger.error(e.getMessage(), e);
	        }
	        return info;
	}

}
